<template>

 	<div class="layui-layout layui-layout-admin" >
 	  <div class="adminnav1">
        <Pagetop></Pagetop>
      </div>
      <div class="adminnav2">
       	<Secondnav urlname="news"></Secondnav>
      </div>

      <!-- 主体内容 -->
      <div class="layui-body kedou-admin" id="LAY_app_body app">
	    <div class="layadmin-tabsbody-item layui-show">
	    	<div class="pagelist">
	    		<div class="layui-card-body">
		      		<fieldset class="layui-elem-field layui-field-title">
					  <legend>资讯列表</legend>
					</fieldset>
		      	</div>
	    		<div class="pagetop">
	    			<Search :seadate="searchArr"></Search>
		        </div>
		        <div class="layui-card-body">
		        	<div class="buttop">
					    <button type="button" class="layui-btn  layui-btn-normal" @click="addData">添加</button>
					</div>
			        <Tables :fdate="pdate" :result="res" height="430" :totalCount="totalRows" :pageNumber="curindex" :fbtn="btn"></Tables>
		        </div>
	        </div>
	    </div>
	  </div>

	</div>
</template>
<script>
	import Pagetop from '../common/pagetop.vue'
	import Secondnav from '../common/secondnav.vue'
	import Tables from '../common/tables.vue'
	import Search from '../common/search.vue'
	export default {
	  name: 'memberlist',
	  components: {Pagetop,Secondnav,Tables,Search},
	  data () {
	    return {
	        pdate:[], //表头
	    	res:"", //内容
	    	totalRows:"",//总数量
	    	curindex:1, //页数
	    	btn:[],//按钮
	    	searchArr:[],//搜索
	    	isShow:false,
	    	seapram:'',
	    	type:localStorage.getItem("type"),
	    }
	  },
	  mounted(){
	  	this.getdate();
     	this.getsearch();
	  },
	  methods: {
		getdate:function(){
    	var _this=this;
    	if(_this.type=='1'){
    		var url="/advert/aapi/v1.9/news"
    	}else{
    		var url="/advert/bapi/v1.9/news/dealer/query"
    	}
    	if(_this.seapram){
    		var dates={
                size:pageSize,
	    		page:this.curindex,
	           sort:'createAt,DESC',
	           'status.equals':'ENABLE'
	    	}
    		var res=_this.seapram;
    		for(var i=0;i<res.length;i++){
    			dates[res[i].key+'.contains']=res[i].val;
    		}

    	}else{
	    	var dates={
               size:pageSize,
	    	   page:this.curindex,
	           sort:'createAt,DESC',
	           'status.equals':'ENABLE'
		    }
      }

      //alert(url+"//"+JSON.stringify(dates))
    	ajaxRequest(url,'GET','json',dates,function(ret,err){
    		//alert(url+"//"+JSON.stringify(ret))
	  		var arr=[];
		  	arr[0]=[
		       {field: 'id', title: 'ID',width:'200'}
		      ,{field: 'title', title: '标题',width:'500'}
		      ,{field: 'category', title: '所属栏目',width:'200',templet:function(d){
		       		return d.category.categoryName
		       }}
		      ,{field: 'deep', title: '状态',width:'100',templet:function(d){
		       		if(d.status=='ENABLE'){
		       			return '启用'
		       		}
		       		else{
		       			return '禁用'
		       		}
		       }}
		      ,{field: 'readingQuantity', title: '阅读量',width:'100',}
		      ,{field: 'creatorName', title: '创建人',width:'200',}
		      ,{field: 'createAt', title: '发布时间',width:'200',templet:function(d){return FormatDate(d.createAt,3)}}
		      ,{field: 'right',title: '操作', width:'300', fixed:'right',templet:function(d){
		         var str='<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">编辑</a>';
             if(d.recommend==2){
               str=str+'<a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="enable">取消推荐</a>';
             }
             else{
              str=str+'<a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="enable">推荐</a>';
             }
				 str=str+'<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>';
				 str=str+'<a class="layui-btn layui-btn-xs" lay-event="detail">添加营销模版</a>';
		         return str;
		      }}
		    ];
		  	_this.pdate=arr;
		  	_this.res=ret.data.list;
		  	_this.totalRows=ret.data.totalRows;
		  	_this.curindex=ret.data.page+1;
	    })
    },
    getsearch:function(){
    	var arr=[
    		{field: 'title', title: '标题',type: 'input'}
    	];
    	this.searchArr=arr;
    },
    page:function(index,obj){
    	//alert(JSON,stringify(obj))
    	this.curindex=index;
    	this.getdate();

    },
    sousuo:function(pram){
    	//alert(JSON.stringify(pram))
    	this.seapram=pram;
    	this.res='';
    	this.totalRows='';
    	this.curindex='';
    	this.getdate();
    },
	screens:function(){
		return screen();
	},
    delInfo:function(data){
	    var _this=this;
	    layer.confirm('是否确认删除？', function(){
	      if(_this.type=='1'){
	        var url='/advert/aapi/v1.9/news/'+data.id
	      }else{
	      	var url='/advert/bapi/v1.9/news/'+data.id
	      }
	      ajaxRequest(url,'DELETE','json','',function(ret,err){
	      	layer.msg("操作成功");
	      	setTimeout(function(){
	      	  	_this.getdate();
	        },1000)
	      })
	
	    });
    },
  enableInfo:function(data){
     var _this=this;
     if(data.recommend==2){
       var recommend=1
     }
     else{
       var recommend=2
     }
     if(_this.type=='1'){
        var url='/advert/aapi/v1.9/news/recommend?id='+data.id+'&recommend='+recommend
     }else{
     	var url='/advert/bapi/v1.9/news/recommend?id='+data.id+'&recommend='+recommend
     }
     ajaxRequest(url,'PUT','json','',function(ret,err){
      layer.msg("操作成功");
      setTimeout(function(){
          _this.getdate();
       },1000)
     })

  },
	addData:function(){
		var _this = this;
		layer.open({
      type: 2
      ,title: '新增资讯'
      ,content:baseurl+'/newsadd'
      ,area:['60%', '70%']
    });
	},
  editInfo:function(data){
  	var _this = this;
  	layer.open({
      type: 2
      ,title: '编辑资讯'
      ,content:baseurl+'/newsadd'
      ,area: ['60%', '70%']
      ,success : function(layero, index){
        var iframeWin = window[layero.find('iframe')[0]['name']];
        iframeWin.getdates(data);
      },
    });
   },
     detailInfo: function(data) {
		 var _this = this;
      if(_this.type == '1'){
		var id = data.id;
      this.$router.push({
        path: "/newsyx",
        query: {
          id: id
        }
      });
      }else{
        layer.msg("该角色无权添加营销模版")
      }
    },
  },

  updated:function(){
  	this.isShow = true;
	layui.use('form', function(){
	  var form = layui.form;
	  form.render();
	});
  },
}
</script>
<style scoped>
/**/
</style>
